package com.example.practice;


import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment_4 extends Fragment {
    private TextView textView;
    private Button button;
    private View view;
    private Spinner spinner4;
    private ListView list4;
    private List<Car3Users> list_r = new ArrayList<>();
    public Fragment_4() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_4, container, false);
        initView();
        initData();
        listView();
        list4.setAdapter(new myAdapter());
        return view;
    }

    private void listView() {

    }

    private void initData() {
        SQLiteDatabase db = getActivity().openOrCreateDatabase("info", getActivity().MODE_PRIVATE, null);
        Cursor info1 = db.query("charge", null, null,null, null, null, null);
        int i=0;
        if(info1!= null){
            while(info1.moveToNext()) {
                Car3Users data = new Car3Users();
                i++;
                data.setRecNo(i+"");
                data.setCharge_user(info1.getString(info1.getColumnIndex("charge_user")));
                data.setCharge_caron(info1.getString(info1.getColumnIndex("charge_carno")));
                data.setCharge_amount(info1.getString(info1.getColumnIndex("charge_amount")));
                data.setCharge_data(info1.getString(info1.getColumnIndex("charge_data")));
                list_r.add(data);
            }
        }
        if(i==0){
            textView.setText("暂无历史记录");
        }
        final String[] arr = getResources().getStringArray(R.array.languagess);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (getActivity(), android.R.layout.test_list_item, arr);
        spinner4.setAdapter(adapter);
        spinner4.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i){
                case 0:
                Collections.sort(list_r, new Comparator<Car3Users>() {
                    @Override
                    public int compare(Car3Users car3Users, Car3Users t1) {
                        return car3Users.getCharge_data().compareTo(t1.getCharge_data());
                    }
                });
                    break;
                case 1:
                    Collections.sort(list_r, new Comparator<Car3Users>() {
                        @Override
                        public int compare(Car3Users car3Users, Car3Users t1) {
                            return t1.getCharge_data().compareTo(car3Users.getCharge_data());
                        }
                    });
                break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    private void initView() {
        spinner4 = (Spinner) view.findViewById(R.id.spinner4);
        list4 = (ListView) view.findViewById(R.id.list4);
        button = view.findViewById(R.id.qx4);
        textView = view.findViewById(R.id.text4);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list4.setAdapter(new myAdapter());
            }
        });
    }
    private class myAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return list_r.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }
        private class ViewHolder{
            TextView textView0;
            TextView textView1;
            TextView textView2;
            TextView textView3;
            TextView textView4;
        }
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if(view==null){
                view=LayoutInflater.from(getContext()).inflate(R.layout.layout_item,null);
                holder=new ViewHolder();
                holder.textView0 = view.findViewById(R.id.item_id);
                holder.textView1 = view.findViewById(R.id.item_car);
                holder.textView2 = view.findViewById(R.id.item_amount);
                holder.textView3 = view.findViewById(R.id.item_user);
                holder.textView4 = view.findViewById(R.id.item_time);
                view.setTag(holder);
            }else {
                holder= (ViewHolder) view.getTag();
            }
            holder.textView0.setText(list_r.get(i).getRecNo());
            holder.textView1.setText(list_r.get(i).getCharge_caron());
            holder.textView2.setText(list_r.get(i).getCharge_amount());
            holder.textView3.setText(list_r.get(i).getCharge_user());
            holder.textView4.setText(list_r.get(i).getCharge_data());
            return view;
        }
    }
}
